//
//  MYHomeViewController.swift
//  MYWeiBo
//
//  Created by apple on 16/5/9.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit

class MYHomeViewController: MYBaseViewController {
    private let reuseIdentifier = "cell"
    var statuses:[MYStatus]?{
        didSet{
            self.tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
     
        //判断是否登录
        if !MYUserAccountModel.sharedInstance.isUserLogin{
            MYPrint(items: "用户没有登录")
            return
        }
        setNavigationbarItem()
        //注册cell
        tableView.registerClass(MYhomeViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        //设置cell的预估行高
        tableView.estimatedRowHeight = 200
        //根据cell的约束来设置contentview的高
        tableView.rowHeight = UITableViewAutomaticDimension
        //打通数据，加载微博数组
        //取消分隔线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        MYStatusViewModel.sharedInstance.loadStatus { (statuses, error) -> () in
            if error != nil{
                 MYPrint(items: "请求数据出错")
                 return
            }
//            MYPrint(items: "在控制器里打印请求得到的数据：\(statuses)")
            self.statuses = statuses
            
        }
    }
    private func setNavigationbarItem(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action:"didClickFriendSearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didClickPop")
        //定义中间按钮
        let button = MYHomeMiddleButton()
        button.setImage(UIImage(named:"navigationbar_arrow_down"), forState: UIControlState.Normal)
        button.setTitle("用户名称", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        button.sizeToFit()
        button.addTarget(self, action: "didClickMiddleButton:", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = button
     
    }
    func didClickMiddleButton(button:UIButton){
        //点击了中间的按钮
        button.selected = !button.selected
        //旋转
        UIView.animateWithDuration(0.5) { () -> Void in
            let transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)): CGAffineTransformIdentity
            button.imageView?.transform = transform
        }
    }
    func didClickFriendSearch(){
    
        MYPrint(items: "点了左边按钮")
    }
    func didClickPop(){
    
        MYPrint(items: "点了右边按钮")
    }
   
}

extension MYHomeViewController{
    //重写tableview的代理方法
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //取得Cell
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MYhomeViewCell
        //取得模型
        let status = self.statuses![indexPath.row]
        //设置内容
        cell.status = status
        //返回cell
        return cell
    }




}
